package com.app.core.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;

import com.android.tu.loadingdialog.LoadingDailog;

public class BaseDialogFragment extends DialogFragment {
    protected LoadingDailog.Builder loadBuilder;
    protected LoadingDailog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * 为获取接口类型定义的一个辅助方法 简化每次都要强转的麻烦
     * @param listenerInterface
     * @param <T>
     * @return
     */
    protected <T> T getDialogListener(Class<T> listenerInterface) {
        //用targetFragment是否为空来标识是fragment还是activity开启的这个DialogFragment
        final Fragment targetFragment = getTargetFragment();
        if (targetFragment != null && listenerInterface.isAssignableFrom(targetFragment.getClass())) {
            return (T) targetFragment;
        }
        if (getActivity() != null && listenerInterface.isAssignableFrom(getActivity().getClass())) {
            return ((T) getActivity());
        }
        return null;
    }

    /**
     * 加载框
     * @param text
     */
    public void showDialog(String text) {
        loadBuilder = new LoadingDailog.Builder(getActivity())
                .setMessage(text)
                .setCancelable(true)
                .setCancelOutside(false);
        dialog = loadBuilder.create();
        dialog.show();


    }

    /**
     * 关闭加载框
     */
    public void closeDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
